package demo3;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-11
 * Time: 15:25
 */
class Solution {
    public static int removeElement(int[] nums, int val) {
        //定义两个指针：
        int src = 0;//用来装非val的值
        int dest = 0;//作为新的数组指针
        while(src < nums.length - 1) {
            if(nums[src] == val) {
                //假如 nums数组中的元素是val，srcz指针就直接跳过，
                src++;
            }else {
                nums[dest++] = nums[src++];
            }
        }
        return dest;
    }
}
class Solution1 {
    public static int removeElement(int[] nums, int val) {
        int ans = 0;
        for(int num: nums) {
            if(num != val) {
                nums[ans] = num;
                ans++;
            }
        }
        return ans;
    }
}
public class Homework {
    public static void main(String[] args) {
        //int[] nums = new int[]{2};
        int[] nums = {3, 2, 2, 3};
        int ret = Solution.removeElement(nums, 3);
        //int[] ret1 = Solution.removeElement(nums, 3);
        System.out.println(ret);
        System.out.println(Arrays.toString(nums));//[2, 2, 2, 3]

        int ret1 = Solution1.removeElement(nums, 3);
        System.out.println(ret1);
        System.out.println(Arrays.toString(nums));
    }
}
